package com.zhuiyun.project.api.userrecord.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.caseImage.entity.CaseImage;
import com.zhuiyun.project.api.caseImage.mapper.CaseImageMapper;
import com.zhuiyun.project.api.caseRecord.entity.CaseRecord;
import com.zhuiyun.project.api.caseRecord.mapper.CaseRecordMapper;
import com.zhuiyun.project.api.coachmanagement.entity.CoachManagement;
import com.zhuiyun.project.api.coachmanagement.mapper.CoachManagementMapper;
import com.zhuiyun.project.api.memberUserManagement.entity.MemberUserManagement;
import com.zhuiyun.project.api.memberUserManagement.mapper.MemberUserManagementMapper;
import com.zhuiyun.project.api.plan.fitnessPlan.entity.FitnessPlan;
import com.zhuiyun.project.api.plan.fitnessPlan.mapper.FitnessPlanMapper;
import com.zhuiyun.project.api.reservation.entity.Reservation;
import com.zhuiyun.project.api.reservation.mapper.ReservationMapper;
import com.zhuiyun.project.api.userrecord.entity.UserRecord;
import com.zhuiyun.project.api.userrecord.mapper.UserRecordMapper;
import com.zhuiyun.project.api.userrecord.model.AddCaseRecordsModel;
import com.zhuiyun.project.api.userrecord.model.UploadUserRecordModel;
import com.zhuiyun.project.api.userrecord.service.UserRecordService;
import com.zhuiyun.project.api.vipPlan.entity.MemberPlan;
import com.zhuiyun.project.api.vipPlan.mapper.VipPlanMapper;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.constants.CommonConstants;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName UserRecordController
 * @Description TODO
 * @Author wuwan
 * @Date 2023/11/20 14:51
 **/
@RestController
@RequestMapping("/UserRecord")
public class UserRecordController {
    @Autowired
    UserRecordService userRecordService;

    @Resource
    UserRecordMapper userRecordMapper;
    @Resource
    CoachManagementMapper coachManagementMapper;
    @Resource
    ReservationMapper reservationMapper;
    @Resource
    VipPlanMapper vipPlanMapper;
    @Resource
    FitnessPlanMapper fitnessPlanMapper;
    @Resource
    MemberUserManagementMapper memberUserManagementMapper;
    @Resource
    CaseImageMapper caseImageMapper;

    /*
     * @Author wuwan
     * @Description 从已完成训练 上传到学员课程记录
     * @Date 2023/11/20 15:08
     * @Return
     */
    @PostMapping("/uploadUserRecord")
    @ApiOperation(value = "上传学员课程记录")
    @OptLog(modular = "教练端课程记录",type = CommonConstants.SELECT,describe = "上传学员课程记录")
    public CommonResult uploadUserRecord(@RequestBody UploadUserRecordModel uploadUserRecordModel) throws CommonException {
        return CommonResult.ok(userRecordService.uploadUserRecord(uploadUserRecordModel));
    }
    /*
     * @Author wuwan
     * @Description 上传学员课程图片
     * @Date 2023/11/20 15:08
     * @Return
     */
    @PostMapping("/uploadImg")
    @ApiOperation(value = "上传学员课程图片")
    @OptLog(modular = "教练端课程记录",type = CommonConstants.SELECT,describe = "上传学员课程图片")
    public String uploadImg(@RequestBody MultipartFile file) {
        return userRecordService.uploadImg(file);
    }
    /*
     * @Author wuwan
     * @Description 教练查询全部用户记录
     * @Date 2023/11/21 15:32
     * @Return
     */
    @GetMapping("/selectUserInformation")
    @ApiOperation(value = "教练查询全部用户记录")
    @OptLog(modular = "教练端课程记录",type = CommonConstants.SELECT,describe = "教练查询全部用户记录")
    public CommonResult selectUserInformation(Integer recordId){
        Integer coachId =recordId;
        return CommonResult.ok(userRecordService.selectTeacherCase(coachId));
    }
    /*
     * @Author wuwan
     * @Description 提交案例
     * @Date 2023/11/22 10:41
     * @Return
     */
    @PostMapping("/addCaseRecords")
    @ApiOperation(value = "提交案例")
    @OptLog(modular = "教练端课程记录",type = CommonConstants.SELECT,describe = "提交案例")
    public CommonResult addCaseRecords(@RequestBody AddCaseRecordsModel addCaseRecordsModel){
        return CommonResult.ok(userRecordService.addCaseRecords(addCaseRecordsModel));
    }
    /*
     * @Author wuwan
     * @Description 上传案例开始时图片
     * @Date 2023/11/20 15:08
     * @Return
     */
    @PostMapping("/uploadStartPhoto")
    @ApiOperation(value = "上传案例开始时图片")
    @OptLog(modular = "教练端课程记录",type = CommonConstants.SELECT,describe = "上传案例开始时图片")
    public String uploadStartPhoto(@RequestBody MultipartFile file) {
        return userRecordService.uploadStartPhoto(file);
    }
    /*
     * @Author wuwan
     * @Description 上传案例结束时图片
     * @Date 2023/11/20 15:08
     * @Return
     */
    @PostMapping("/uploadEndingPhoto")
    @ApiOperation(value = "上传案例结束时图片")
    @OptLog(modular = "教练端课程记录",type = CommonConstants.UPDATE,describe = "上传案例结束时图片")
    public String uploadEndingPhoto(@RequestBody MultipartFile file) {
        return userRecordService.uploadEndingPhoto(file);
    }
    /*
     * @Author wuwan
     * @Description 查看案例
     * @Date 2023/11/24 8:58
     * @Return
     */
    @GetMapping("/selectUserCase")
    @ApiOperation(value = "查看案例")
    @OptLog(modular = "教练端案例记录",type = CommonConstants.UPDATE,describe = "查看案例")
    // uid用户id  tid教练id  caseStatus记录状态0审核中1已上传
    public CommonResult selectUserCase(Integer tid,Integer caseStatus){
        return CommonResult.ok(userRecordService.selectUserCase(tid,caseStatus));
    }

    /*
     * @Author xy
     * @Description todo 案例查询
     * @Date 2023/11/24 9:31
     * @Return
     * @param
     */
    @ApiOperation(value = "学员案例查询")
    @OptLog(modular = "案例管理", type = CommonConstants.SELECT, describe = "学员案例查询 ")
    @GetMapping("/selectUserRecord")
    public CommonResult listCase(@RequestParam Integer coachId,@RequestParam Integer userId) {
        // 根据前端传过来的coachId（教练id）来查询案例表id符合的数据
        QueryWrapper<UserRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id",coachId);
        queryWrapper.eq("member_user_id",userId);

        List<UserRecord> userRecords = userRecordMapper.selectList(queryWrapper);
        return CommonResult.ok(userRecords);
    }

    /**
    * @Author wx
    * @Description 根据学员编号 教练编号查询案例
    * @Date 2023/12/4 13:53
    * @Return
    * @param coachId,memberId
    */
    @ApiOperation(value = "学员案例查询")
    @OptLog(modular = "案例管理", type = CommonConstants.SELECT, describe = "学员案例查询 ")
    @GetMapping("/selectTeacherCase")
    public CommonResult selectTeacherCase(@RequestParam String venueUniqueIdentifier,@RequestParam Integer coachId,@RequestParam Integer memberUserId){
        return CommonResult.ok(userRecordService.selectTeacherCase(coachId));
    }

    /**
    * @Author wx
    * @Description 修改案例评分
    * @Date 2023/12/12 14:48
    * @Return
    * @param fraction
    */
    @ApiOperation(value = "修改案例评分")
    @OptLog(modular = "案例管理", type = CommonConstants.SELECT, describe = "修改案例评分 ")
    @GetMapping("/updateCsaeFroction")
    public CommonResult updateCsaeFroction(@RequestParam String fraction,@RequestParam Integer recordId,@RequestParam Integer memberUserId){
        Integer coachId = recordId;
        return CommonResult.ok(userRecordService.updateCsaeFroction(fraction,coachId,memberUserId));
    }

    /*
    * @Author xy
    * @Description todo 案例查询
    * @Date 2023/12/14 14:12
    * @Return
    * @param
    */
    @ApiOperation(value = "学员案例查询")
    @OptLog(modular = "案例管理", type = CommonConstants.SELECT, describe = "学员案例查询 ")
    @GetMapping("/selectUserRecordByUserId")
    public CommonResult selectUserRecordByUserId(PageRequest pageRequest, @RequestParam String venueUniqueIdentifier) {
        MybatisPageHelper.startPage(pageRequest);
        List<UserRecord> userRecords = userRecordMapper.selectUserRecordByUserId(pageRequest,venueUniqueIdentifier);
        PageResult pageResult = MybatisPageHelper.getPageResult(userRecords);
        return CommonResult.ok(pageResult);
    }
}